using System.Threading.Tasks;
using Admin.NET.Application.AttributeInfo;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Application.Service.Rules.Dto;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Admin.NET.Application;

/// <summary>
/// 水价方案服务
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class WaterPriceRuleService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<WaterPriceRule> _rep;

	public WaterPriceRuleService(SqlSugarRepository<WaterPriceRule> rep)
	{
		_rep = rep;
	}

	/// <summary>
	/// 分页查询电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/waterPriceRule/page")]
	public async Task<dynamic> Page([FromQuery] WaterPriceRuleInput input)
	{
		ISugarQueryable<WaterPriceRule> query = _rep.Context.Queryable<WaterPriceRule>().WhereIF(!string.IsNullOrWhiteSpace(input.Name), (WaterPriceRule u) => u.Name.Contains(input.Name.Trim())).WhereIF(!string.IsNullOrWhiteSpace(input.Description), (WaterPriceRule u) => u.Description.Contains(input.Description.Trim()));
		query = query.OrderBuilder(input);
		return await query.ToPagedListAsync(input.Page, input.PageSize);
	}

	/// <summary>
	/// 增加电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/waterPriceRule/add")]
	public async Task Add(AddWaterPriceRuleInput input)
	{
		WaterPriceRule entity = input.Adapt<WaterPriceRule>();
		await _rep.InsertAsync(entity);
	}

	/// <summary>
	/// 删除电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/waterpriceRule/delete")]
	public async Task Delete(DeletePriceRuleInput input)
	{
		WaterPriceRule entity = await _rep.GetFirstAsync((WaterPriceRule u) => u.Id == input.Id);
		await _rep.DeleteAsync(entity);
	}

	/// <summary>
	/// 更新电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/waterPriceRule/edit")]
	[OperationLog("更新电价方案", "name")]
	public async Task Update(UpdateWaterPriceRuleInput input)
	{
		WaterPriceRule entity = input.Adapt<WaterPriceRule>();
		await _rep.UpdateAsync(entity);
	}

	/// <summary>
	/// 获取电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/waterPriceRule/detail")]
	public async Task<WaterPriceRule> Get([FromQuery] QueryePriceRuleInput input)
	{
		return await _rep.GetFirstAsync((WaterPriceRule u) => u.Id == input.Id);
	}
}
